//
//  ResetPasswordViewController.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/12.
//  Copyright © 2018 王右. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "AddProjectInfoEditCell.h"
#import "IQKeyboardManager.h"

@interface ResetPasswordViewController ()

@property (strong, nonatomic) IBOutlet UITableView *baseTableView;
@property (strong, nonatomic) IBOutlet UIView *bottmView;
@property (strong, nonatomic) IBOutlet UIButton *completeButton;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSMutableArray *placehorderArr;
@property (nonatomic, strong) NSMutableArray *uploadStringArr;
@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"密码修改";
    _titleArr = @[@"账号",@"原密码",@"新密码",@"确认新密码"];
    _placehorderArr = @[@"请输入用户名",@"请输入旧密码",@"请输入新密码",@"请确认新密码"].mutableCopy;
    if (User_Account.length) {
        _uploadStringArr = @[User_Account,@"",@"",@""].mutableCopy;
    }else{
        _uploadStringArr = @[@"",@"",@"",@""].mutableCopy;
    }
    _baseTableView.tableFooterView = _bottmView;
    _completeButton.layer.cornerRadius = 22.5;
    _completeButton.layer.masksToBounds = YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddProjectInfoEditCell *cell = (AddProjectInfoEditCell *)[tableView cellWithCellNibName:@"AddProjectInfoEditCell"];
    cell.nameLabel.text = _titleArr[indexPath.row];
    if ([_uploadStringArr[indexPath.row] length] == 0) {
        cell.detailTextField.placeholder = _placehorderArr[indexPath.row];
        cell.detailTextField.enabled = YES;
    }else{
        cell.detailTextField.text = _uploadStringArr[indexPath.row];
    }
    cell.block = ^(NSString * _Nonnull textString) {
        if (textString != nil) {
            [self->_uploadStringArr replaceObjectAtIndex:indexPath.row withObject:textString];
        }else{
            [self->_uploadStringArr replaceObjectAtIndex:indexPath.row withObject:@""];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (IBAction)completeAction:(id)sender {
    
    NSString *userAccount = _uploadStringArr[0];
    NSString *oldPassword = _uploadStringArr[1];
    NSString *newPassword = _uploadStringArr[2];
    NSString *confirmNewPassword = _uploadStringArr[3];
    if (userAccount.length==0) {
        [self.view makeToast:@"请输入用户名!"];
        return;
    }
    if (oldPassword.length==0) {
        [self.view makeToast:@"请输入旧密码!"];
        return;
    }
    if (newPassword.length==0) {
        [self.view makeToast:@"请输入新密码!"];
        return;
    }
    if (![newPassword isEqualToString:confirmNewPassword]) {
        [self.view makeToast:@"两次密码输入不一致!"];
        return;
    }
    [self hudWithText:@"修改中..."];
    [RequestManager postRequestWithModule:@"v1/updatepwd" param:@{@"account":userAccount,@"oldpwd":oldPassword,@"newpwd":newPassword} model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        if ([obj[@"result"][@"check_result"] boolValue]) {
            [[UIApplication sharedApplication].delegate.window makeToast:@"密码修改成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self.view makeToast:@"密码修改失败!"];
        }
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}

@end
